package com.example.administrator.acmapp.Activity;

import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.acmapp.Assist.CustomVideoView;
import com.example.administrator.acmapp.Fragment.LoginAndSiginFragmentPage.LoginFragment;
import com.example.administrator.acmapp.Fragment.LoginAndSiginFragmentPage.SiginFragment;
import com.example.administrator.acmapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/31/031.
 */

public class LoginActivity extends FragmentActivity {
    private CustomVideoView customVideoView;
    private Button button;
    List<Fragment> fragmentList;
    private SiginFragment siginFragment;
    private LoginFragment loginFragment;
    private ViewPager viewPager;
    private TextView mSiginNav;
    private TextView mLoginNav;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.login_activity);
        initView();
    }
    private void initView(){
        mLoginNav = findViewById(R.id.login_login);
        mSiginNav = findViewById(R.id.login_sigin);
        viewPager = findViewById(R.id.login_viewpage);
        fragmentList = new ArrayList<>();
        loginFragment = new LoginFragment();
        siginFragment = new SiginFragment();
        fragmentList.add(loginFragment);
        fragmentList.add(siginFragment);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        mLoginNav.setBackgroundResource(R.drawable.login_sigin_shape);
                        mSiginNav.setBackground(Drawable.createFromPath("@null"));
                        break;
                    case 1:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        customVideoView = findViewById(R.id.videoview);
        customVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.loginmv));
        //播放
        customVideoView.start();
        //循环播放
        customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                customVideoView.start();
            }
        });
    }
    //返回重启加载
    @Override
    protected void onRestart() {
        initView();
        super.onRestart();
    }

    //防止锁屏或者切出的时候，音乐在播放
    @Override
    protected void onStop() {
        customVideoView.stopPlayback();
        super.onStop();
    }
}
